<?php
class Dbsms {

    protected $main_table = 'api_mt_gd';
	protected $main_table2 = 'api_rpt_gd';
    protected $main_id = 'AUTO_SN';
    protected $db = null;

    public function __construct() {
		//load_file('mysql_class', LIB_PATH);
		$db2 = new MyPDO('202.105.136.57', 'gd', 'gdcoship?', 'mas','mysql',"latin1");		//服务器latin1码
		$this->db = $db2;
    }

    public function __destruct() {
	unset($this->db);
    }

    public function get_table_name() {
	return $this->main_table;
    }

    /**
     * 	insert news
     * 	@param array $data 
     * 	@return int affected count
     */
    public function insert_data($data) {
	if (!is_array($data))
	    my_jump('输入的参数有误');
	$rows = $this->db->insert($this->main_table, $data);
	$insert_id = $this->db->getInsertId();
	return $insert_id;
    }

    /**
     * 	update news
     * 	@param array $data 
     * 	@param string $where 
     * 	@return int affected count
     */
    public function update_data($data, $where) {
	if (!is_array($data))
	    my_jump('输入的参数有误');
	$result = $this->db->update($this->main_table, $data, $where);
	return $result;
    }

    /**
     * 	delete news
     * 	@param string $where 
     * 	@return int affected count
     */
    public function del_data($where) {
	$result = $this->db->delete($this->main_table, $where);
	return $result;
    }

  
    public function get_data_row($id, $field = '*') {
	if (!intval($id))
	    my_jump('输入的参数有误!');
	$sql = "SELECT " . $field . " FROM " . $this->main_table . " WHERE " . $this->main_id . "='" . intval($id) . "' ";
	$data = $this->db->getRow($sql);
	return $data;
    }

	public function send_sms($mobiles,$content,$time=''){
		if($mobiles){
			//INSERT into api_mt_gd (SM_ID,SRC_ID,MOBILES,CONTENT,SEND_TIME) values ('123','123','13428678258','SM test',NOW());" 
			$insert = array(
				'sm_id'=>'0',
				'src_id'=>'0',			//来源尾号显示
				'mobiles'=>$mobiles,
				'content'=>mb_convert_encoding($content, "gbk","utf-8"), //服务器latin1码
				'send_time'=>$time
			);	
			$rs = $this->insert_data($insert);
			return $rs;
		}
		
	}
   
   	public function get_reply($phone){
		$sql = "select * from " . $this->main_table2 . " where MOBILE = '$phone' order by AUTO_SN desc"; 
		$data =$this->db->getRow($sql);
		if($data){
			$data['RPT_DESC'] = mb_convert_encoding($data['RPT_DESC'],"utf-8","gbk");
		} 		
	   	return $data;
	   
   	}
	
	public function get_reply_list($id=''){
	$sql = "select * from " . $this->main_table2 . " limit 10"; 
	$data =$this->db->getAll($sql); 
	if($data){
		foreach($data as $k=>$v){
			$data[$k]['RPT_DESC'] = mb_convert_encoding($v['RPT_DESC'],"utf-8","gbk");
		}
	}
	return $data;
   
}


}

?>